package com.yanqu.road.dao.impl.activity.soulbone;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.soulbone.SoulBoneUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class SoulBoneUserDataDaoImpl extends TempDataObjectDao<SoulBoneUserData> {

    public static String getSoulBoneUserDataSql = "select * from `t_u_soul_bone_user_data` where `activityId`=? and `userId`=?";
    public SoulBoneUserData getSoulBoneUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSoulBoneUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateSoulBoneUserDataSql = "update `t_u_soul_bone_user_data` set totalDrawTimes=?,drawTimes=?,sixTimes=?,juBao=?,score=?,getItemMap=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(SoulBoneUserData soulBoneUserData) {
        soulBoneUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, soulBoneUserData.getTotalDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, soulBoneUserData.getDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, soulBoneUserData.getSixTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, soulBoneUserData.getJuBao()));
        para.put(i++, new DbParameter(Types.BIGINT, soulBoneUserData.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(soulBoneUserData.getGetItemMap())));
        para.put(i++, new DbParameter(Types.INTEGER, soulBoneUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, soulBoneUserData.getUserId()));
        if (getDb().executeNoneQuery(updateSoulBoneUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSoulBoneUserDataSql = "insert into `t_u_soul_bone_user_data`(activityId,userId,totalDrawTimes,drawTimes,sixTimes,juBao,score,getItemMap) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(SoulBoneUserData soulBoneUserData) {
        soulBoneUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, soulBoneUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, soulBoneUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, soulBoneUserData.getTotalDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, soulBoneUserData.getDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, soulBoneUserData.getSixTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, soulBoneUserData.getJuBao()));
        para.put(i++, new DbParameter(Types.BIGINT, soulBoneUserData.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(soulBoneUserData.getGetItemMap())));
        if (getDb().executeNoneQuery(insertSoulBoneUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SoulBoneUserData parseData(ResultSet rs) throws SQLException {
        SoulBoneUserData soulBoneUserData = new SoulBoneUserData();
        soulBoneUserData.setActivityId(rs.getInt("activityId"));
        soulBoneUserData.setUserId(rs.getLong("userId"));
        soulBoneUserData.setTotalDrawTimes(rs.getInt("totalDrawTimes"));
        soulBoneUserData.setDrawTimes(rs.getInt("drawTimes"));
        soulBoneUserData.setSixTimes(rs.getInt("sixTimes"));
        soulBoneUserData.setJuBao(rs.getString("juBao"));
        soulBoneUserData.setScore(rs.getLong("score"));
        soulBoneUserData.setGetItemMap(JSON.parseObject(rs.getString("getItemMap"), new TypeReference<Map<Integer, Integer>>() {}));
        return soulBoneUserData;
    }
}
